package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysAudit;
import com.ccp.dev.system.service.SysAuditService;
import com.ccp.dev.core.basic.annotion.Action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 系统审计,操作日志 信息操作处理控制器
 * 
 * @author zw
 * @date 2019-08-05 19:43:22
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/sysAudit")
@Action(ownermodel = SysAuditModelType.SYSTEM_SETTING)
public class SysAuditController extends SysBaseController{
	@Resource
	private SysAuditService sysAuditService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-08-05 19:43:22
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="操作日志首页页面跳转",exectype = "页面跳转日志")
    public String list(HttpServletRequest request,ModelMap map) {
        String logLeave = RequestUtil.getString(request,"logLeave");
        map.put("logLeave",logLeave);
        return "sysAudit/list";
    }

    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-08-05 19:43:22
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="操作日志新增页面页面跳转",exectype = "页面跳转日志")
    public String add(HttpServletRequest request,ModelMap modelMap) {
        String auditid = RequestUtil.getString(request,"auditid");
        modelMap.put("auditid",auditid);
        return "sysAudit/add";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: sysAudit 保存的对象
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-08-05 19:43:22
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="保存操作日志数据",
            detail = "<#if add>添加<#else>更新</#if>操作日志数据<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    public Object save(@RequestBody SysAudit sysAudit) {
        String id = sysAudit.getAuditid();
        int result;
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("add", true);
            //新增操作
            //新增数据前生成随机Auditid
            sysAudit.setAuditid(UUIDUtils.getUUIDFor32());
            result = sysAuditService.add(sysAudit);
        } else {
            SysAuditThreadLocalHolder.putParamerter("add", false);
            //修改操作
            result = sysAuditService.update(sysAudit);
        }
        if(result > 0 ){
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("save.success");
        }else{
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("save.failure");
        }
    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"auditid"
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-08-05 19:43:22
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除操作日志数据",
            detail = "<#if success>根据主键ID删除【姓名：${entity.executor}，时间：${entity.exetime?string('yyyy-MM-dd HH:mm:ss')},操作：${entity.opname}】操作日志数据成功<#else>根据主键ID删除失败</#if>",
            exectype = "删除日志")
    public Object delete(@RequestParam("id") String auditid) {
        if (StringUtil.isEmpty(auditid)) {
            return fail("delete.failure.message");
        }
        SysAudit sysAudit = sysAuditService.getOneById(auditid);
        SysAuditThreadLocalHolder.putParamerter("entity", sysAudit);
        int result = sysAuditService.delByIdArray(auditid.split(","));
        if(result > 0 ){
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("delete.success");
        }else{
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
    }
    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"auditid"
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-08-05 19:43:22
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description="根据主键id查询操作日志的一条数据",
            detail = "<#if success>查询【姓名：${entity.executor}，时间：${entity.exetime?string('yyyy-MM-dd HH:mm:ss')},操作：${entity.opname}】成功<#else>id为空查询失败</#if>",
            exectype = "查询日志")
    public Object get(@RequestParam("id") String auditid) {
        SysAudit sysAudit = sysAuditService.getOneById(auditid);
        if (null == sysAudit) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("get.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        SysAuditThreadLocalHolder.putParamerter("entity", sysAudit);
        return getResultDataSuccess("get.success",sysAudit);
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-08-05 19:43:22
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询操作日志",exectype = "查询日志")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        String logLeave = RequestUtil.getString(request,"logLeave");
        //分页数据
        List<SysAudit> pageDatas = sysAuditService.queryList(queryFilter,logLeave);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }

    @RequestMapping("/view/chart")
    @Action(description="系统日志页面统计图表",exectype = "查询日志")
    public String chart(HttpServletRequest request,ModelMap modelMap) {
        String logLeave = RequestUtil.getString(request,"logLeave");
        List<SysAudit> sysAudits = sysAuditService.queryLogToEchart(logLeave);
        List<String> ownerModel = new ArrayList<>();
        List<Integer> countLog = new ArrayList<>();
        String ownerModelToString = "";
        String countLogToString = "";
        if(sysAudits.size() > 0){
            ownerModelToString = "[";
            countLogToString = "[";
            for (SysAudit sysAudit:sysAudits) {
                ownerModel.add(sysAudit.getOwnermodel());
                ownerModelToString = ownerModelToString + "'"+sysAudit.getOwnermodel()+"'"+',';
                countLog.add(sysAudit.getCount());
                countLogToString = countLogToString +sysAudit.getCount()+',';
            }
            ownerModelToString = ownerModelToString.substring(0,ownerModelToString.length()-1) + "]";
            countLogToString = countLogToString.substring(0,countLogToString.length()-1) + "]";
        }
        modelMap.put("ownerModel",ownerModelToString);
        modelMap.put("countLog",countLogToString);
        return "sysAudit/chart";
    }



	
}
